import { EnumSchema } from '@/utils/common/types';
import { PresetColorEnum } from '@/enums/colorEnum';
import { useI18n } from '@/hooks/web/useI18n';

/**  操作结果类型 */
export enum AjaxResultTypeEnum {
  // 2XX
  /** 成功 */
  Success = 200,
  // 4XX
  /** 错误 */
  Error = 400,
  /** 未登录 */
  Unauthorized = 401,
  /** 已登录但权限不足 */
  Forbidden = 403,
  /** 资源未找到 */
  NotFound = 404,
  /** 不允许 */
  NotAllowed = 405,
  /** 不支持 */
  Unsupported = 415,
  /** 资源被锁定 */
  Locked = 423,
  // 5XX
  /** 服务器内部错误 */
  InternalError = 500,
  /** 功能未实现 */
  NotImplemented = 501,
}

/** 操作结果类型实体 */
export class AjaxResultTypeEntity {
  /** 操作结果类型 */
  ResultType: AjaxResultTypeEnum;
  /** 显示描述 */
  Display: string;
  /** 颜色 */
  Color?: PresetColorEnum | string;

  constructor(resultType: AjaxResultTypeEnum, color?: PresetColorEnum | string) {
    const { t } = useI18n();
    this.ResultType = resultType;
    this.Color = color;
    switch (resultType) {
      case AjaxResultTypeEnum.Success:
        this.Display = t('enum.ajaxResultType.success');
        this.Color = this.Color ? this.Color : PresetColorEnum.Green;
        break;
      case AjaxResultTypeEnum.Error:
        this.Display = t('enum.ajaxResultType.error');
        this.Color = this.Color ? this.Color : PresetColorEnum.Red;
        break;
      case AjaxResultTypeEnum.Unauthorized:
        this.Display = t('enum.ajaxResultType.unauthorized');
        this.Color = this.Color ? this.Color : PresetColorEnum.Orange;
        break;
      case AjaxResultTypeEnum.Forbidden:
        this.Display = t('enum.ajaxResultType.forbidden');
        this.Color = this.Color ? this.Color : PresetColorEnum.Orange;
        break;
      case AjaxResultTypeEnum.NotFound:
        this.Display = t('enum.ajaxResultType.notFound');
        this.Color = this.Color ? this.Color : PresetColorEnum.Pink;
        break;
      case AjaxResultTypeEnum.NotAllowed:
        this.Display = t('enum.ajaxResultType.notAllowed');
        this.Color = this.Color ? this.Color : PresetColorEnum.Pink;
        break;
      case AjaxResultTypeEnum.Unsupported:
        this.Display = t('enum.ajaxResultType.unsupported');
        this.Color = this.Color ? this.Color : PresetColorEnum.Pink;
        break;
      case AjaxResultTypeEnum.Locked:
        this.Display = t('enum.ajaxResultType.locked');
        this.Color = this.Color ? this.Color : PresetColorEnum.Pink;
        break;
      case AjaxResultTypeEnum.InternalError:
        this.Display = t('enum.ajaxResultType.internalError');
        this.Color = this.Color ? this.Color : PresetColorEnum.Red;
        break;
      case AjaxResultTypeEnum.NotImplemented:
        this.Display = t('enum.ajaxResultType.notImplemented');
        this.Color = this.Color ? this.Color : PresetColorEnum.Magenta;
        break;
    }
  }
}

//#region 获取操作结果类型枚举架构
/**
 * 获取操作结果类型枚举架构
 */
export function getAjaxResultTypeEnumSchema(enableTag = true): EnumSchema[] {
  const enumSchema: EnumSchema[] = [];
  const entities: AjaxResultTypeEntity[] = [
    new AjaxResultTypeEntity(AjaxResultTypeEnum.Success),
    new AjaxResultTypeEntity(AjaxResultTypeEnum.Error),
    new AjaxResultTypeEntity(AjaxResultTypeEnum.Unauthorized),
    new AjaxResultTypeEntity(AjaxResultTypeEnum.Forbidden),
    new AjaxResultTypeEntity(AjaxResultTypeEnum.NotFound),
    new AjaxResultTypeEntity(AjaxResultTypeEnum.NotAllowed),
    new AjaxResultTypeEntity(AjaxResultTypeEnum.Unsupported),
    new AjaxResultTypeEntity(AjaxResultTypeEnum.Locked),
    new AjaxResultTypeEntity(AjaxResultTypeEnum.InternalError),
    new AjaxResultTypeEntity(AjaxResultTypeEnum.NotImplemented),
  ];
  entities.forEach((item) => {
    enumSchema.push({
      key: item.ResultType as number,
      label: item.Display,
      value: item.ResultType as number,
      tag: enableTag,
      tagColor: item.Color,
    });
  });
  return enumSchema;
}
//#endregion
